import { Image as PDFImage } from "@react-pdf/renderer";
import { styles } from "../styles";

export const ResumePDFImage = ({
                                   src = "/images/default-bg.jpg", // 默认使用绝对路径
                                   style,
                                   isPDF
                               }: {
    src?: string;
    style?: any;
    isPDF: boolean;
}) => {
    // 处理路径逻辑
    const finalSrc = (() => {
        try {
            if(src.startsWith('blob:')) return src;
            // 开发环境直接使用绝对路径
            if (process.env.NODE_ENV === 'development') {
                return src.startsWith('/')
                    ? src
                    : `/images/${src}`;
            }
            // 生产环境拼接PUBLIC_URL
            return `${process.env.PUBLIC_URL || ''}${src.startsWith('/') ? src : `/${src}`}`;
        } catch {
            return "/images/default-bg.jpg";
        }
    })();
    console.log("finalSrc:", finalSrc);

    return isPDF ? (
        <PDFImage
            src={finalSrc}
            style={[styles.a4, style]}
            debug={false}
        />
    ) : (
        <img
            src={finalSrc}
            style={{ ...style, pointerEvents: 'none' }}
            alt="background"
            onError={(e) => {
                e.currentTarget.src = '/images/default-bg.jpg';
            }}
        />
    );
};